package com.cangxuan.app.ui.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cangxuan.app.R;
import com.cangxuan.app.entity.Orderform;
import com.cangxuan.app.ui.BaseActivity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class OrderformActivity extends BaseActivity {

    /**
     * 文章类数据适配器
     */
    class OrderformAdapter extends BaseAdapter {

        private List<Orderform> orderformList;

        public OrderformAdapter(List<Orderform> orderformList) {
            this.orderformList = orderformList;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        @Override
        public int getCount() {
            return orderformList.size();
        }

        @Override
        public Object getItem(int i) {
            return orderformList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return orderformList.get(i).getId();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //声明视图持有类实例
            ViewHolder viewHolder;
            if (view == null) {
                //实例view对象
                view = LayoutInflater.from(getApplicationContext()).inflate(
                        R.layout.activity_orderform_list_item, viewGroup, false);
                //当ListView第一次获取项的视图时
                viewHolder = new ViewHolder();
                //给viewHolder中的属性（控件）进行初始化
                viewHolder.txtOrderformGoodsName = view.findViewById(R.id.txt_orderform_goods_name);
                viewHolder.txtGoodsPrice = view.findViewById(R.id.txt_goods_price);
                viewHolder.txtGoodsCode = view.findViewById(R.id.txt_goods_code);
                viewHolder.txtOrderformState = view.findViewById(R.id.txt_orderform_state);
                viewHolder.txtOrderformCode = view.findViewById(R.id.txt_orderform_code);
                viewHolder.txtOrderformCreateTime = view.findViewById(R.id.txt_orderform_create_time);
                viewHolder.txtOrderformPayTime = view.findViewById(R.id.txt_orderform_pay_time);
                viewHolder.txtOrderformCompleteTime = view.findViewById(R.id.txt_orderform_complete_time);
                //将viewHolder设置在view的tag属性中，用于下一次getView时，即可从view的tag属性中获取。
                view.setTag(viewHolder);
            } else {
                //当ListView后面再获取项的视图时
                viewHolder = (ViewHolder) view.getTag();
            }
            //先取出对应下标的Orderform
            Orderform orderform = orderformList.get(i);
            //给viewHolder中的控件绑定数据
            viewHolder.txtOrderformGoodsName.setText(orderform.getGoodsName());
            viewHolder.txtGoodsPrice.setText(String.valueOf(orderform.getPrice()));
            viewHolder.txtGoodsCode.setText(orderform.getGoodsCode());
            viewHolder.txtOrderformState.setText(orderform.getStatus());
            viewHolder.txtOrderformCode.setText(orderform.getOrderCode());
            viewHolder.txtOrderformCreateTime.setText(orderform.getCreateTime().format(formatter));
            viewHolder.txtOrderformPayTime.setText(orderform.getPayTime().format(formatter));
            viewHolder.txtOrderformCompleteTime.setText(orderform.getCompleteTime().format(formatter));
            return view;
        }

        /**
         * 视图持有类，
         * 该类的属性会包含ListView的项里面需要动态设置值的控件
         */
        class ViewHolder{
            TextView txtOrderformGoodsName;
            TextView txtGoodsPrice;
            TextView txtGoodsCode;
            TextView txtOrderformState;
            TextView txtOrderformCode;
            TextView txtOrderformCreateTime;
            TextView txtOrderformPayTime;
            TextView txtOrderformCompleteTime;
        }

    }

    TextView txtOrderformStateWaitPay;
    TextView txtOrderformStateComplete;
    TextView txtOrderformStateCancel;

    //文章列表
    ListView listviewOrderform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderform);
        setTitle("我的订单");

        //绑定控件
        txtOrderformStateWaitPay = findViewById(R.id.txt_orderform_state_wait_pay);
        txtOrderformStateComplete = findViewById(R.id.txt_orderform_state_complete);
        txtOrderformStateCancel = findViewById(R.id.txt_orderform_state_cancel);
        listviewOrderform = findViewById(R.id.listview_orderform);

        //创建要显示的数据源
        List<Orderform> orderformList = new ArrayList<>();
        //本来在这里应该调用API接口获取数据，现在暂时添加一些测试数据
        orderformList.add(new Orderform(45, "JDJFHJSYSFJ","第1号藏品", 100F,"SVYHF6Y686","已完成", LocalDateTime.now(), LocalDateTime.now(), LocalDateTime.now()));
        orderformList.add(new Orderform(45, "FVHHG4FGFDG","第2号藏品", 200F,"XFDSGHH3","已完成", LocalDateTime.now(), LocalDateTime.now(), LocalDateTime.now()));
        orderformList.add(new Orderform(45, "XGFHHJRFRGG","第3号藏品", 300F,"FDSY43S","已完成", LocalDateTime.now(), LocalDateTime.now(), LocalDateTime.now()));

        //创建文章适配器
        OrderformAdapter orderformAdapter = new OrderformAdapter(orderformList);
        //给listviewOrderform绑定适配器
        listviewOrderform.setAdapter(orderformAdapter);

        txtOrderformStateWaitPay.setOnClickListener(view -> {
            System.out.println("点击了待付款选项");
        });
        txtOrderformStateComplete.setOnClickListener(view -> {
            System.out.println("点击了已完成选项");
        });
        txtOrderformStateCancel.setOnClickListener(view -> {
            System.out.println("点击了已取消选项");
        });
    }
}